//app.js
App({
    onLaunch: function () {
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)

        var openid = wx.getStorageSync('openid') || '';
        var userInfo = wx.getStorageSync('userInfo') || '';

        self = this;

        if (!openid) {
            // 登录
            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    wx.request({
                        url: self.apiURL + 'auth/getopenid?code=' + res.code,
                        data: {},
                        method: 'GET',
                        // header: {}, // 设置请求的 header
                        success: function (res) {
                            console.log(res);
                            if (res.data.errno == 0) {
                                var obj = {};
                                obj.openid = res.data.data.openid;
                                obj.expires_in = Date.now() + res.data.expires_in;
                                wx.setStorageSync('openid', obj.openid);//存储openid
                                self.globalData.openid = obj.openid;
                            }
                        }
                    });
                    /*
                    wx.getUserInfo({
                      success: function (res) {
                        var objUser = {};
                        objUser.avatarUrl = res.userInfo.avatarUrl;
                        objUser.nickName = res.userInfo.nickName;
                        //console.log(objz);
                        self.globalData.userInfo = objUser;
                        wx.setStorageSync('userInfo', objUser);//存储userInfo
                      }
                    });
                    */
                }
            })
        } else {
            self.globalData.openid = openid;
        }

        // 获取用户信息(废弃)
        //this.initUserInfo();

    },

    apiURL: 'https://www.leuar.com/wx/api/',
    //apiURL: 'http://127.0.0.1:8000/wx/api/',
    globalData: {
        userInfo: null,
        openid: null
    }
})